<?php

namespace App\Http\Middleware;
use DB;
use Closure;

class LoginMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if($request -> session() -> has('id')){
            $id = session('id');
            $res = DB::table('userdetail') -> where('id',$id) -> first();
            if($res){
                session([
                    'name' => $res -> username,
                ]);
            }else{
                session() -> forget('id');
            }
        }
        return $next($request);
    }
}
